#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Create your views here.

import json

import redis

from django.http import JsonResponse
from django.views import View

from vehicle.models import Vehicle_info, Vehicle_fence, Vehicle_fault_info, \
    Vehicle_fault_log, Vehicle_move_info

from vehicle.serializers import (Vehicle_InfoModelSerializer,
                                 Vehicle_fenceModelSerializer,
                                 Vehicle_fault_infoModelSerializer,
                                 Vehicle_fault_logModelSerializer,
                                 Vehicle_move_infoModelSerializer
                                 )

from dvadmin.utils.viewset import CustomModelViewSet


class VehicleInfoModelViewSet(CustomModelViewSet):
    """
    车辆信息表
    """
    queryset = Vehicle_info.objects.all()
    serializer_class = Vehicle_InfoModelSerializer


class Vehicle_fenceModelViewSet(CustomModelViewSet):
    """
    电子围栏
    """
    queryset = Vehicle_fence.objects.all()
    serializer_class = Vehicle_fenceModelSerializer
    filter_fields = ['fence_name', 'fence_alarm_type', 'status']
    search_fields = ['fence_name']


class Vehicle_fault_infoModelViewSet(CustomModelViewSet):
    """
    故障表
    """
    queryset = Vehicle_fault_info.objects.all()
    serializer_class = Vehicle_fault_infoModelSerializer
    filter_fields = []
    search_fields = []


class Vehicle_fault_logModelViewSet(CustomModelViewSet):
    """
    故障日志
    """
    queryset = Vehicle_fault_log.objects.all()
    serializer_class = Vehicle_fault_logModelSerializer
    filter_fields = []
    search_fields = []


class Vehicle_move_infoModelViewSet(CustomModelViewSet):
    """
    车辆行驶记录
    """
    queryset = Vehicle_move_info.objects.all()
    serializer_class = Vehicle_move_infoModelSerializer
    filter_fields = ['vehicle_vin']
    search_fields = []


pool = redis.ConnectionPool(host='127.0.0.1', port=6379, decode_responses=True)
r = redis.Redis(connection_pool=pool)
VEHICLE_DATA_DICT = {
    "vin": "vin",
    "motorCurrent": "电机电流",
    "motorSpeed": "电机转速",
    "motorTemperature": "电机温度",
    "motorTorque": "电机转矩",
    "motorVoltage": "电机电压",
    "gear": "档位",
    "remainingBattery": "剩余电量",
    "brakePedal": "制动踏板行程值",
    "fuelConsumptionRate": "燃料消耗率",
    "resistance": "绝缘电阻",
    "singleBatteryMaxTemperature": "单体电池最高温度",
    "singleBatteryMaxVoltage": "单次最大电压",
    "singleBatteryMinTemperature": "单体电池最低温度",
    "singleBatteryMinVoltage": "单体电池最低电压",
    "totalBatteryCurrent": "动力电池充放电电流",
    "totalBatteryVoltage": "动力电池负载端总电压V3",
}


class VehicleMove_info(View):
    def get(self, request, pk, ):
        # print(pk)
        common_keys = (
            "latitude",
            "longitude",
            "speed",
            "voltage",
            "singleBatteryMinTemperature"
        )

        data = r.lpop(pk)
        print(type(data))
        real_info = dict()

        if data:
            real_info = json.loads(data)
            print(type(real_info))
            common_info = {k: v for k, v in real_info.items() if k in common_keys}
            real_info = {v: real_info.get(k) for k, v in VEHICLE_DATA_DICT.items()}
            real_info.update(common_info)

        return JsonResponse(dict(code=2000, data=real_info))
